<template>
	<view class="container">
		<!--<view class="list-cell b-b m-t" @click="navTo('个人资料')" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">个人资料</text>
			<text class="cell-more yticon icon-you"></text>
		</view>
		<view class="list-cell b-b" @click="navTo('收货地址')" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">收货地址</text>
			<text class="cell-more yticon icon-you"></text>
		</view>
		<view class="list-cell" @click="navTo('实名认证')" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">实名认证</text>
			<text class="cell-more yticon icon-you"></text>
		</view>
		<view class="list-cell b-b" @click="navTo('关于我们')" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">关于Dcloud</text>
			<text class="cell-more yticon icon-you"></text>
		</view>
		<view class="list-cell m-t">
			<text class="cell-tit">消息推送</text>
			<switch checked color="#fa436a" @change="switchChange" />
		</view>
		-->
		
		
		<view class="list-cell m-t b-b" @click="clearSession" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">清除缓存</text>
			<text class="cell-more yticon icon-you"></text>
		</view>
		<view class="list-cell m-t b-b" @click="navTo('/pages/set/payPass')" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">修改支付密码</text>
			<text class="cell-more yticon icon-you"></text>
		</view>
		<view class="list-cell b-b" @click="navTo('/pages/set/loginPass')" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">修改登录密码</text>
			<text class="cell-more yticon icon-you"></text>
		</view>
		<view class="list-cell" @click="updateVersion">
			
			<text class="cell-tit">检查更新</text>
			
			<text class="cell-tip" style="color: red;font-weight: 800;padding-right: 5upx;" v-if="showNotice">·</text>
			<text class="cell-tip">当前版本 {{version.version}}</text>
			<text class="cell-tip padding-left-xs" v-if="showNotice">有新版本</text>
			<text class="cell-more yticon icon-you"></text>
		</view>
		<view class="list-cell log-out-btn" @click="toLogout">
			<text class="cell-tit">退出登录</text>
		</view>
	</view>
</template>

<script>
	import {  
	    mapMutations  
	} from 'vuex';
	export default {
		data() {
			return {
				version:{},
				newVersion:{},
				showNotice:false
			};
		},
		onShow() {
			this.version = this.$version;
		},
		methods:{
			
			updateVersion(){
				var that = this;
				uni.request({
					url: that.$host+'version.json',
					method: 'GET',
					dataType:'json',
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'X-Requested-With': 'XMLHttpRequest'
					},
					success: function(res) {
						that.newVersion = res.data;
						if(that.version.version != res.data.version){
							that.showNotice = true;
							uni.showModal({
								title:'新版本',
							    content: '是否立即更新',
								confirmText:'立即更新',
								cancelText:'暂不更新',
							    success: (e)=>{
							    	if(e.confirm){
										//获取系统信息
										uni.getSystemInfo({
										    success: function (res) {
										        let plat = res.platform;
												if('ios' == plat){
													plus.runtime.openURL(that.newVersion.ios);
												}else{
													plus.runtime.openURL(that.newVersion.android);
												}
										    }
										});
							    	}
							    }
							});
						}else{
							this.$api.msg("当前已经是最新版本");
						}
					},
					fail: function(res) {
						console.log(res.data)
					}
				});
			},
			...mapMutations(['logout']),

			navTo(url){
				if(!uni.getStorageInfoSync("TOKEN")){
					uni.redirectTo({
						url: '../index/index'
					}) 
				}else{
					uni.navigateTo({
						url:url
					})
				}
			},
			clearSession(){
				uni.showLoading({
					title:"正在清理缓存",
					success(res) {
						setTimeout(function(){
							uni.hideLoading();
							uni.showToast({
								title:'清理成功',
								icon:'success',
								duration: 1200
							})
						},2000)
					}
				})
			},
			//退出登录
			toLogout(){
				var that = this;
				uni.showModal({
				    content: '确定要退出登录么',
				    success: (e)=>{
				    	if(e.confirm){
				    		that.$network.request("UserAction/exit", {}, function(res) {
				    			if(res.data.state){
									uni.removeStorageSync("TOKEN")
									uni.getStorageSync("TOKEN")
									uni.navigateBack({})
								}
				    		}, "", function(res) {
				    			console.error(JSON.stringify(res))
				    			uni.showToast({
				    				icon: 'none',
				    				title: '加载失败',
				    				duration: 1200
				    			})
				    		});
							
							
				    		setTimeout(()=>{
				    			uni.redirectTo({
				    				url: '../index/index'
				    			})();
				    		}, 200)
				    	}
				    }
				});
			},
			//switch
			switchChange(e){
				let statusTip = e.detail.value ? '打开': '关闭';
				this.$api.msg(`${statusTip}消息推送`);
			},

		}
	}
</script>

<style lang='scss'>
	page{
		background: $page-color-base;
	}
	.list-cell{
		display:flex;
		align-items:baseline;
		padding: 20upx $page-row-spacing;
		line-height:60upx;
		position:relative;
		background: #fff;
		justify-content: center;
		&.log-out-btn{
			margin-top: 40upx;
			.cell-tit{
				color: $uni-color-primary;
				text-align: center;
				margin-right: 0;
			}
		}
		&.cell-hover{
			background:#fafafa;
		}
		&.b-b:after{
			left: 30upx;
		}
		&.m-t{
			margin-top: 16upx; 
		}
		.cell-more{
			align-self: baseline;
			font-size:$font-lg;
			color:$font-color-light;
			margin-left:10upx;
		}
		.cell-tit{
			flex: 1;
			font-size: $font-base + 2upx;
			color: $font-color-dark;
			margin-right:10upx;
		}
		.cell-tip{
			font-size: $font-base;
			color: $font-color-light;
		}
		switch{
			transform: translateX(16upx) scale(.84);
		}
	}
</style>
